const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ageIndex: 7,
    ageArr: ['男', '女'],
    educateIndex: 1,
    educateArr: ['本科', '专科'],
    birthday: '',
    is_hidden: false,
    userInformation: {},
    addressMenuIsShow: false,
    value: [],
    provinces: [],
    citys: [],
    areaInfo: '',
    old_value: [],
    old_citys: [],

    addressMenuIsShow_1: false,
    value_1: [],
    provinces_1: [],
    citys_1: [],
    areaInfo_1: '',
    old_value_1: [],
    old_citys_1: [],
  },
  inputUpdate: function (e) {

    var item = e.currentTarget.dataset.item;
    if (item == 'name' || item == 'sex') {
      wx.navigateTo({
        url: '../auth/auth',
      });
      return;
    }

    console.log('UPDATE ITEM:' + item);
    wx.navigateTo({
      url: './update/update?item=' + item,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    wx.request({
      url: app.globalData.apiDomain + "crowdsource/lookup/getProvinceInfo",
      data: {
        parentId: ''
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          var xiaoquList = res.data.object;

          var xiaoquArr = xiaoquList.map(item => {
            return item.displayname;
          });
          var xiaoquIds = xiaoquList.map(item => {
            return item.id;
          });

          var xiaoqu_id = _this.data.userInformation.nativePlaceProvince ? _this.data.userInformation.nativePlaceProvince : 22;
          var xiaoqu_index = xiaoquIds.indexOf(xiaoqu_id);
          if(xiaoqu_index == -1) {
            xiaoqu_index = 0;
          }

          var xiaoqu_id_1 = _this.data.userInformation.residencePlaceProvince ? _this.data.userInformation.residencePlaceProvince : 22;
          var xiaoqu_index_1 = xiaoquIds.indexOf(xiaoqu_id_1);
          if (xiaoqu_index_1 == -1) {
            xiaoqu_index_1 = 0;
          }

          _this.setData({
            provinces: xiaoquArr,
            provinces_1: xiaoquArr,
            xiaoquList,
            xiaoquList_1:xiaoquList
          });

          _this.setData({
            value: [xiaoqu_index, 0],
            old_value: [xiaoqu_index, 0],
            value_1: [xiaoqu_index_1, 0],
            old_value_1: [xiaoqu_index_1, 0]
          });

          if (xiaoqu_id) {
            _this.cityList(xiaoqu_id, true)
            _this.cityList_1(xiaoqu_id_1, true)
          }
        }else {
          app.responseCheck(_this, res.data, '/pages/user/userinformation/userinformation')
        }
      },
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },

  cityList: function (xiaoqu_id, is_init) {
    var _this = this;
    if (xiaoqu_id) {

      wx.request({
        url: app.globalData.apiDomain + "crowdsource/lookup/getProvinceInfo",
        data: {
          'parentId': xiaoqu_id
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.status == 'ok') {
            var classList = res.data.object;
            var classArr = classList.map(item => {
              return item.displayname;
            })
            var classIds = classList.map(item => {
              return item.id;
            })
            if (is_init) {
              console.log('下拉值初始化-1');
              var class_id = _this.data.userInformation.nativePlace;
              var class_index = classIds.indexOf(class_id);
              if(class_index == -1) {
                class_index = 0;
              }

              _this.data.value[1] = class_index;
              _this.setData({
                citys: classArr,
                old_citys: classArr,
                classList,
                old_classList: classList
              });
              _this.setData({
                value: _this.data.value,
                old_value: _this.data.value
              });
              var areaInfo = _this.data.provinces[_this.data.value[0]] + ',' + _this.data.citys[_this.data.value[1]];
              _this.setData({
                areaInfo: areaInfo
              });
            } else {
              console.log('下拉值改变-1');
              _this.setData({
                citys: classArr,
                classList
              })
            }
          }else{
            app.responseCheck(_this, res.data, '/pages/user/userinformation/userinformation')
          }
        },
        fail: function (e) {
          wx.showModal({
            content: '网络异常,请重试',
            showCancel: false
          })
        }
      })
    }
  },

  cityList_1: function (xiaoqu_id, is_init) {
    var _this = this;
    if (xiaoqu_id) {

      wx.request({
        url: app.globalData.apiDomain + "crowdsource/lookup/getProvinceInfo",
        data: {
          'parentId': xiaoqu_id
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.status == 'ok') {
            var classList_1 = res.data.object;
            var classArr = classList_1.map(item => {
              return item.displayname;
            })
            var classIds = classList_1.map(item => {
              return item.id;
            })
            if (is_init) {
              console.log('下拉值初始化-2');
              var class_id = _this.data.userInformation.residencePlace;
              var class_index = classIds.indexOf(class_id);
              if (class_index == -1) {
                class_index = 0;
              }

              _this.data.value_1[1] = class_index;
              _this.setData({
                citys_1: classArr,
                old_citys_1: classArr,
                classList_1,
                old_classList_1: classList_1
              });
              _this.setData({
                value_1: _this.data.value_1,
                old_value_1: _this.data.value_1
              });
              var areaInfo = _this.data.provinces[_this.data.value_1[0]] + ',' + _this.data.citys_1[_this.data.value_1[1]];
              _this.setData({
                areaInfo_1: areaInfo
              });
            } else {
              console.log('下拉值改变-2');
              _this.setData({
                citys_1: classArr,
                classList_1
              })
            }
          } else {
            app.responseCheck(_this, res.data, '/pages/user/userinformation/userinformation')
          }
        },
        fail: function (e) {
          wx.showModal({
            content: '网络异常,请重试',
            showCancel: false
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData);
    this.setData({
      'birthday': app.globalData.userInformation.birthday ? app.globalData.userInformation.birthday : '2016-09-10',
      'userInformation': app.globalData.userInformation
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 性别
  ageUpdate: function (e) {
    var age = e.detail.value;
    this.setData({
      ageIndex: age
    });
  },
  // 籍贯
  countryUpdate: function (e) {
    this.setData({
      is_hiddden: !this.data.is_hidden
    });
  },
  // 生日
  birthdayUpdate: function (e) {
    var _this = this;
    var date = e.detail.value;
    var data = { 'birthday': date };
    _this.updateUser(data, function (res) {
      if (res.data.status == 'ok') {
        app.globalData.userInformation.birthday = date;
        console.log(app.globalData.userInformation);
        _this.setData({
          'userInformation': app.globalData.userInformation,
          'birthday': date
        });
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      }else {
        app.responseCheck(_this, res.data, '/pages/user/userinformation/userinformation')
      }
    });
  },
  // 学历
  educateUpdate: function (e) {
    var educateIndex = e.detail.value;
    this.setData({
      educateIndex: educateIndex
    });
  },
  // 
  addressUpdate: function (e) {
    var educateIndex = e.detail.value;
    this.setData({
      educateIndex: educateIndex
    });
  },

  updateUser: function (data, successFun) {
    var _this = this;
    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/updateWechatUser',
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/json',
        'token': app.globalData.token
      },
      success: successFun,
      fail: function (e) {
        wx.showModal({
          content: '网络异常,请重试',
          showCancel: false
        })
      }
    })
  },

  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    this.startAddressAnimation(true)
  },
  startAddressAnimation: function (isShow) {
    var that = this
    this.setData({
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false);

    this.setData({
      classList: this.data.old_classList
    });

    // this.setData({
    //   provinces: this.data.old_provinces,
    // })

    console.log(this.data.old_value);
    console.log(this.data.value);

    this.setData({
      value: [this.data.old_value[0], 0]
    });
    this.setData({
      citys: this.data.old_citys,
    })
    this.setData({
      value: [this.data.old_value[0], this.data.old_value[1]]
    });
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var _this = this;
    this.startAddressAnimation(false)
    var areaInfo = this.data.provinces[this.data.value[0]] + ',' + this.data.citys[this.data.value[1]]
    var province_id = this.data.xiaoquList[this.data.value[0]].id;
    var city_id = this.data.classList[this.data.value[1]].id;
    console.log(province_id);
    console.log(city_id);
    // http提交
    var data = { nativePlace: city_id};
    this.updateUser(data,function(res) {
      if (res.data.status == 'ok') {
        app.globalData.userInformation.nativePlaceProvince = province_id;
        app.globalData.userInformation.nativePlace = city_id;
        console.log(app.globalData.userInformation);
       
        _this.setData({
          areaInfo: areaInfo,
          old_value: _this.data.value,
          old_citys: _this.data.citys,
          old_classList: _this.data.classList
        })

        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      }else {
        app.responseCheck(_this, res.data, '/pages/user/userinformation/userinformation')
      }
    })
  },
  // 点击蒙版时取消组件的显示
  hideCitySelected: function (e) {
    this.startAddressAnimation(false);
    this.setData({
      classList: this.data.old_classList
    });

    this.setData({
      value: [this.data.old_value[0], 0]
    });
    this.setData({
      citys: this.data.old_citys,
    })
    this.setData({
      value: [this.data.old_value[0], this.data.old_value[1]]
    });
  },

  // 处理省市县联动逻辑
  cityChange: function (e) {
 
    console.log("值改变触发时间-1")
    console.log(e);
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var provinceNum = value[0]
    var cityNum = value[1]

    if (this.data.value[0] != provinceNum) {
      console.log('one');
      var xiaoquList = this.data.xiaoquList;
      var provinces_id = xiaoquList[provinceNum].id;
      this.cityList(provinces_id);
      this.setData({
        value: [provinceNum, 0]
      })
    } else if (this.data.value[1] != cityNum) {
      console.log('two');
      this.setData({
        value: [provinceNum, cityNum]
      })
    } else {
      console.log('three');
    }
  },


  // 2222222点击所在地区弹出选择框2222222222
  selectDistrict1: function (e) {
    this.startAddressAnimation1(true)
  },
  startAddressAnimation1: function (isShow) {
    var that = this
    this.setData({
      addressMenuIsShow_1: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel1: function (e) {
    this.startAddressAnimation1(false);

    this.setData({
      classList_1: this.data.old_classList_1
    });

    console.log(this.data.old_value_1);
    console.log(this.data.value_1);

    this.setData({
      value_1: [this.data.old_value_1[0], 0]
    });
    this.setData({
      citys_1: this.data.old_citys_1,
    })
    this.setData({
      value_1: [this.data.old_value_1[0], this.data.old_value_1[1]]
    });
  },
  // 点击地区选择确定按钮
  citySure1: function (e) {
    var _this = this;
    this.startAddressAnimation1(false)
    var areaInfo = this.data.provinces_1[this.data.value_1[0]] + ',' + this.data.citys_1[this.data.value_1[1]]
    var province_id = this.data.xiaoquList_1[this.data.value_1[0]].id;
    var city_id = this.data.classList_1[this.data.value_1[1]].id;
    console.log(province_id);
    console.log(city_id);
    // http提交
    var data = { residencePlace: city_id };
    this.updateUser(data, function (res) {
      if (res.data.status == 'ok') {
        app.globalData.userInformation.residencePlaceProvince = province_id;
        app.globalData.userInformation.residencePlace = city_id;
        //console.log(app.globalData.userInformation);

        _this.setData({
          areaInfo_1: areaInfo,
          old_value_1: _this.data.value_1,
          old_citys_1: _this.data.citys_1,
          old_classList_1: _this.data.classList_1
        })

        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      }else{
        app.responseCheck(_this, res.data, '/pages/user/userinformation/userinformation')
      }
    })
  },
  // 点击蒙版时取消组件的显示
  hideCitySelected1: function (e) {
    this.startAddressAnimation1(false);

    this.setData({
      classList_1: this.data.old_classList_1
    });

    console.log(this.data.old_value_1);
    console.log(this.data.value_1);

    this.setData({
      value_1: [this.data.old_value_1[0], 0]
    });
    this.setData({
      citys_1: this.data.old_citys_1,
    })
    this.setData({
      value_1: [this.data.old_value_1[0], this.data.old_value_1[1]]
    });
  },

  // 处理省市县联动逻辑
  cityChange1: function (e) {

    console.log("值改变触发时间-2")
    console.log(e);
    var value = e.detail.value
    var provinces = this.data.provinces_1
    var citys = this.data.citys_1
    var provinceNum = value[0]
    var cityNum = value[1]

    if (this.data.value_1[0] != provinceNum) {
      console.log('one');
      var xiaoquList = this.data.xiaoquList_1;
      var provinces_id = xiaoquList[provinceNum].id;
      this.cityList_1(provinces_id);
      this.setData({
        value_1: [provinceNum, 0]
      })
    } else if (this.data.value_1[1] != cityNum) {
      console.log('two');
      this.setData({
        value_1: [provinceNum, cityNum]
      })
    } else {
      console.log('three');
    }
  },


})